<template>
  <div v-loading="containerLoading">
    <div class="filter-container">
      <el-select v-model="userSelDepa" size="mini" placeholder="选择系部" class="filter-item" style="width: 130px" clearable @change="getList">
        <el-option
          v-for="item in config.departmentOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <el-select
        v-model="activityGrade"
        placeholder="选择年级"
        clearable
        size="mini"
        style="width: 130px"
        class="filter-item"
        @change="getList">
        <el-option
          v-for="item in config.activityGradeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <el-input v-model="searchContext" placeholder="请根据条件输入内容" class="name-search" size="mini" @change="getList">
        <el-select slot="prepend" v-model="searchAction" placeholder="请选择条件" @change="getList">
          <el-option key="1" value="user_id" label="学号"/>
          <el-option key="2" value="user_name" label="姓名"/>
          <el-option key="3" value="bmmc" label="班级"/>
        </el-select>
      </el-input>

      <el-button
        size="mini"
        type="primary"
        class="filter-item"
        style="margin-left: 10px"
        @click="downloadSignInList">
        导出名单
      </el-button>
      <el-button
        v-if="$store.getters.menuList.hd_add_score && activity.ac_admID === userInfo.user_id && activity.ac_state === 6"
        :disabled="activity.ac_type === 1"
        size="mini"
        type="primary"
        class="filter-item"
        style="margin-left: 10px"
        @click="batchAddonScoreDialog = true">
        批量导入普通活动附加分
      </el-button>

    </div>
    <el-table :data="datalist" size="medium" class="signInUserTable" border>
      <el-table-column
        prop="user_id"
        label="学号"/>
      <el-table-column
        prop="user_name"
        label="姓名"/>
      <el-table-column
        label="系部">
        <template slot-scope="scope">
          <span>{{ scope.row.depa | formatDepa }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="grade"
        label="年级"/>
      <el-table-column
        prop="bmmc"
        label="班级"/>
      <el-table-column
        label="是否认证">
        <template slot-scope="scope">
          <el-tag :type="scope.row.rzbs ? 'success' : 'info'">{{ scope.row.rzbs | foamatVetifyState }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="附加分">
        <template slot-scope="scope">
          <span>{{ scope.row.ac_addscore > 0 ? scope.row.ac_addscore : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="验证码">
        <template slot-scope="scope">
          <span>{{ scope.row.code.length > 1 ? scope.row.code : '-' }}</span>
        </template>
      </el-table-column>
      <!--verify_time-->
      <el-table-column
        width="180px"
        label="验证时间">
        <template slot-scope="scope">
          <span>{{ scope.row.verify_time.length > 1 ? scope.row.verify_time : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="180px"
        label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="$store.getters.menuList.hd_add_score && activity.ac_admID === userInfo.user_id && activity.ac_state === 6"
            :disabled="activity.ac_type === 1"
            type="text"
            size="mini"
            @click="handleAddScore(scope.row.user_id)">
            添加附加分
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="pageTotal > 10"
      :total="pageTotal"
      :current-page.sync="page"
      :page-size="10"
      small
      background
      class="pagination-container"
      layout="prev, pager, next"
      @current-change="getList"/>
    <addscore-dialog :visible="addscoreDialog" :ac-id="acId" :user-id="currentUserId" @update="getList" @close="addscoreDialog = false"/>
    <batch-addon-score-dialog :visible="batchAddonScoreDialog" @importSuccess="getList" @close="batchAddonScoreDialog = false"/>
  </div>

</template>

<script>
import { getsignInUserList } from '@/api/activity'
import config from '@/utils/config.js'
import Pagination from '@/components/Pagination'
import addscoreDialog from './addscoreDialog'
import BatchAddonScoreDialog from './batchAddScoreDialog'

export default {
  name: 'SignInTable',
  components: { Pagination, addscoreDialog, BatchAddonScoreDialog },
  props: {
    acId: {
      type: String,
      required: true
    },
    userInfo: {
      type: Object,
      required: true
    },
    activity: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      datalist: [],
      page: 1,
      pageTotal: 0,
      config,
      userSelDepa: null,
      searchContext: null,
      searchAction: 'user_id',
      activityGrade: null,
      containerLoading: false,
      currentUserId: null,
      addscoreDialog: false,
      batchAddonScoreDialog: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      const data = {}
      data.Ac_Id = this.acId
      data.page = this.page.toString()
      data.size = '10'

      this.containerLoading = true

      getsignInUserList(this.genParams(data))
        .then(res => {
          this.datalist = res.data
          this.pageTotal = res.total
        })
        .finally(() => {
          this.containerLoading = false
        })
    },
    genParams(data) {
      // 增加筛选参数
      if (this.userSelDepa) {
        data.depa = this.userSelDepa.toString()
      }
      if (this.activityGrade) {
        data.grade = this.activityGrade.toString()
      }
      if (this.searchContext) {
        data[this.searchAction] = this.searchContext
      }

      return data
    },
    downloadSignInList() {
      const data = this.genParams({ Ac_Id: this.acId })
      const eleLink = document.createElement('a')
      eleLink.target = '_blank'
      eleLink.href = `/api/score/willnumExport.zepc?${this.json2url(data)}`
      // 触发点击
      document.body.appendChild(eleLink)
      eleLink.click()
      // 然后移除
      document.body.removeChild(eleLink)
    },
    json2url(data) {
      try {
        var tempArr = []
        for (var i in data) {
          var key = encodeURIComponent(i)
          var value = encodeURIComponent(data[i])
          tempArr.push(key + '=' + value)
        }
        var urlParamsStr = tempArr.join('&')
        return urlParamsStr
      } catch (err) {
        return ''
      }
    },
    handleAddScore(userId) {
      this.currentUserId = userId
      this.addscoreDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  margin: 16px 0;
  text-align: right;
}
.filter-container {
  padding-bottom: 0;
}
.name-search {
  width: 280px;
  margin-bottom: 10px;
  vertical-align: middle;
  .el-select {
    width: 80px;
  }
}
.filter-item {
  margin-right: 4px;
}
</style>
